package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sina.weibo.sdk.api.share.Base;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.SubmitOrderTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ToastUtil;

/**
 * Created by XCKJ_Paladowow on 2016/8/23.
 */
public class SubmitOrderActivity extends BaseActivity {
    private Context mContext;
    private Button overBtn;
    private TextView back;
    private String goodsName;
    private String goodsPrice;
    private String goodsImage;
    private TextView mNameText;
    private TextView mPriceText;
    private ImageView mGoodsImage;
    private TextView mChooseText;

    private TextView userName;
    private TextView userPhone;
    private TextView userAddr;

    private String resultName;
    private String resultPhone;
    private String resultAddr;
    private RelativeLayout addrLayout;
    private String goodsId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.submitorder);
        mContext = this;
        initView();
    }

    private void initView() {
        final Intent intent = getIntent();
        if (intent != null) {
            goodsId = intent.getStringExtra("ID");
            goodsName = intent.getStringExtra("NAME");
            goodsPrice = intent.getStringExtra("PRICE");
            goodsImage = intent.getStringExtra("IMAGE");
        }
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        addrLayout = (RelativeLayout) findViewById(R.id.addr_layout);
        userName = (TextView) findViewById(R.id.nickname);
        userPhone = (TextView) findViewById(R.id.phone);
        userAddr = (TextView) findViewById(R.id.sh_addr);

        mNameText = (TextView) findViewById(R.id.goodsname);
        mNameText.setText(goodsName);

        mPriceText = (TextView) findViewById(R.id.need_flour);
        mPriceText.setText(goodsPrice);

        mGoodsImage = (ImageView) findViewById(R.id.head_img);
        if (goodsImage != null && goodsImage.length() > 0) {
            ImageCache.getInstance(mContext).loadBitmapToView(mGoodsImage, goodsImage, null);

        }

        mChooseText = (TextView) findViewById(R.id.choose_btn);

        addrLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(mContext, MyAddressActivity.class);
                startActivityForResult(intent1, 1);
            }
        });

        overBtn = (Button) findViewById(R.id.sure_btn);
        overBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (checkInput()) {
                    doSubmit();
                }
            }
        });
    }

    private void doSubmit() {
        SubmitOrderTask task = new SubmitOrderTask(goodsId, resultName, resultAddr, resultPhone);
        task.addListener(createTaskListner);
        getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener createTaskListner = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            Intent intent = new Intent(mContext, DuiHuanSuccessActivity.class);
            startActivity(intent);
            finish();
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    private boolean checkInput() {
        if (TextUtils.isEmpty(resultAddr)) {
            Toast.makeText(mContext, "请先选择收货地址", Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {
            mChooseText.setVisibility(View.GONE);
            resultName = data.getExtras().getString("NAME");
            resultPhone = data.getExtras().getString("PHONE");
            resultAddr = data.getExtras().getString("ADDR");

            userName.setText(resultName);
            userPhone.setText(resultPhone);
            userAddr.setText(resultAddr);
        }
    }
}
